{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import time\n",
    "from datetime import datetime as dt\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import plotly\n",
    "import plotly.graph_objs as go\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Data Preparation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3 datasets:\n",
    "\n",
    "S&P 500 daily adjusted price\n",
    "\n",
    "Mean-variance weights table\n",
    "\n",
    "Min-variance weights table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_adj = pd.read_csv(\"final_ratios_oct.csv\")\n",
    "weight_meanv = pd.read_excel('mean_weighted_rf.xlsx')\n",
    "weight_minv = pd.read_excel('minimum_weighted_rf.xlsx')\n",
    "weight_equal=pd.read_excel('equally_weighted_rf.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>date</th>\n",
       "      <th>gvkey</th>\n",
       "      <th>tic</th>\n",
       "      <th>gsector</th>\n",
       "      <th>adj_close_q</th>\n",
       "      <th>y_return</th>\n",
       "      <th>EPS</th>\n",
       "      <th>DPS</th>\n",
       "      <th>cur_ratio</th>\n",
       "      <th>quick_ratio</th>\n",
       "      <th>cash_ratio</th>\n",
       "      <th>acc_rec_turnover</th>\n",
       "      <th>debt_ratio</th>\n",
       "      <th>debt_to_equity</th>\n",
       "      <th>pe</th>\n",
       "      <th>ps</th>\n",
       "      <th>pb</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>66261</td>\n",
       "      <td>1998-03-01</td>\n",
       "      <td>126554</td>\n",
       "      <td>A</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.42</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>66262</td>\n",
       "      <td>1998-06-01</td>\n",
       "      <td>126554</td>\n",
       "      <td>A</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.67</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>66263</td>\n",
       "      <td>1998-09-01</td>\n",
       "      <td>126554</td>\n",
       "      <td>A</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.81</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>66264</td>\n",
       "      <td>1998-12-01</td>\n",
       "      <td>126554</td>\n",
       "      <td>A</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.68</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.923077</td>\n",
       "      <td>0.759850</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.909465</td>\n",
       "      <td>0.394024</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>66265</td>\n",
       "      <td>1999-03-01</td>\n",
       "      <td>126554</td>\n",
       "      <td>A</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.19</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77580</th>\n",
       "      <td>41126</td>\n",
       "      <td>2021-09-01</td>\n",
       "      <td>13721</td>\n",
       "      <td>ZTS</td>\n",
       "      <td>35.0</td>\n",
       "      <td>186.36</td>\n",
       "      <td>0.040899</td>\n",
       "      <td>2.25</td>\n",
       "      <td>0.250</td>\n",
       "      <td>3.370636</td>\n",
       "      <td>2.308943</td>\n",
       "      <td>1.749402</td>\n",
       "      <td>4.676923</td>\n",
       "      <td>0.690667</td>\n",
       "      <td>2.232767</td>\n",
       "      <td>172.555556</td>\n",
       "      <td>45.319980</td>\n",
       "      <td>20.306517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77581</th>\n",
       "      <td>41127</td>\n",
       "      <td>2021-12-01</td>\n",
       "      <td>13721</td>\n",
       "      <td>ZTS</td>\n",
       "      <td>35.0</td>\n",
       "      <td>194.14</td>\n",
       "      <td>0.228712</td>\n",
       "      <td>3.42</td>\n",
       "      <td>0.250</td>\n",
       "      <td>4.542488</td>\n",
       "      <td>3.008838</td>\n",
       "      <td>2.225697</td>\n",
       "      <td>4.889757</td>\n",
       "      <td>0.658446</td>\n",
       "      <td>1.927793</td>\n",
       "      <td>167.362069</td>\n",
       "      <td>46.111805</td>\n",
       "      <td>19.632734</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77582</th>\n",
       "      <td>41128</td>\n",
       "      <td>2022-03-01</td>\n",
       "      <td>13721</td>\n",
       "      <td>ZTS</td>\n",
       "      <td>35.0</td>\n",
       "      <td>244.03</td>\n",
       "      <td>-0.257716</td>\n",
       "      <td>4.29</td>\n",
       "      <td>0.250</td>\n",
       "      <td>3.856427</td>\n",
       "      <td>2.569839</td>\n",
       "      <td>1.939343</td>\n",
       "      <td>5.133274</td>\n",
       "      <td>0.673094</td>\n",
       "      <td>2.058979</td>\n",
       "      <td>277.306818</td>\n",
       "      <td>58.539205</td>\n",
       "      <td>25.379013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77583</th>\n",
       "      <td>41129</td>\n",
       "      <td>2022-06-01</td>\n",
       "      <td>13721</td>\n",
       "      <td>ZTS</td>\n",
       "      <td>35.0</td>\n",
       "      <td>188.59</td>\n",
       "      <td>-0.092721</td>\n",
       "      <td>1.26</td>\n",
       "      <td>0.325</td>\n",
       "      <td>2.253461</td>\n",
       "      <td>1.436058</td>\n",
       "      <td>1.033289</td>\n",
       "      <td>4.838789</td>\n",
       "      <td>0.663925</td>\n",
       "      <td>1.975526</td>\n",
       "      <td>149.674603</td>\n",
       "      <td>44.723354</td>\n",
       "      <td>19.077996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77584</th>\n",
       "      <td>41130</td>\n",
       "      <td>2022-09-01</td>\n",
       "      <td>13721</td>\n",
       "      <td>ZTS</td>\n",
       "      <td>35.0</td>\n",
       "      <td>171.89</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.39</td>\n",
       "      <td>0.325</td>\n",
       "      <td>2.179941</td>\n",
       "      <td>1.292363</td>\n",
       "      <td>0.869223</td>\n",
       "      <td>4.608830</td>\n",
       "      <td>0.667393</td>\n",
       "      <td>2.006550</td>\n",
       "      <td>152.115044</td>\n",
       "      <td>39.235211</td>\n",
       "      <td>17.587312</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>77585 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       Unnamed: 0        date   gvkey  tic  gsector  adj_close_q  y_return  \\\n",
       "0           66261  1998-03-01  126554    A     35.0         0.00  0.000000   \n",
       "1           66262  1998-06-01  126554    A     35.0         0.00  0.000000   \n",
       "2           66263  1998-09-01  126554    A     35.0         0.00  0.000000   \n",
       "3           66264  1998-12-01  126554    A     35.0         0.00  0.000000   \n",
       "4           66265  1999-03-01  126554    A     35.0         0.00  0.000000   \n",
       "...           ...         ...     ...  ...      ...          ...       ...   \n",
       "77580       41126  2021-09-01   13721  ZTS     35.0       186.36  0.040899   \n",
       "77581       41127  2021-12-01   13721  ZTS     35.0       194.14  0.228712   \n",
       "77582       41128  2022-03-01   13721  ZTS     35.0       244.03 -0.257716   \n",
       "77583       41129  2022-06-01   13721  ZTS     35.0       188.59 -0.092721   \n",
       "77584       41130  2022-09-01   13721  ZTS     35.0       171.89  0.000000   \n",
       "\n",
       "        EPS    DPS  cur_ratio  quick_ratio  cash_ratio  acc_rec_turnover  \\\n",
       "0      0.42  0.000   0.000000     0.000000    0.000000          0.000000   \n",
       "1      0.67  0.000   0.000000     0.000000    0.000000          0.000000   \n",
       "2      0.81  0.000   0.000000     0.000000    0.000000          0.000000   \n",
       "3      0.68  0.000   1.923077     0.759850    0.000000          4.909465   \n",
       "4      0.19  0.000   0.000000     0.000000    0.000000          0.000000   \n",
       "...     ...    ...        ...          ...         ...               ...   \n",
       "77580  2.25  0.250   3.370636     2.308943    1.749402          4.676923   \n",
       "77581  3.42  0.250   4.542488     3.008838    2.225697          4.889757   \n",
       "77582  4.29  0.250   3.856427     2.569839    1.939343          5.133274   \n",
       "77583  1.26  0.325   2.253461     1.436058    1.033289          4.838789   \n",
       "77584  2.39  0.325   2.179941     1.292363    0.869223          4.608830   \n",
       "\n",
       "       debt_ratio  debt_to_equity          pe         ps         pb  \n",
       "0        0.000000        0.000000    0.000000   0.000000   0.000000  \n",
       "1        0.000000        0.000000    0.000000   0.000000   0.000000  \n",
       "2        0.000000        0.000000    0.000000   0.000000   0.000000  \n",
       "3        0.394024        0.000000    0.000000   0.000000   0.000000  \n",
       "4        0.000000        0.000000    0.000000   0.000000   0.000000  \n",
       "...           ...             ...         ...        ...        ...  \n",
       "77580    0.690667        2.232767  172.555556  45.319980  20.306517  \n",
       "77581    0.658446        1.927793  167.362069  46.111805  19.632734  \n",
       "77582    0.673094        2.058979  277.306818  58.539205  25.379013  \n",
       "77583    0.663925        1.975526  149.674603  44.723354  19.077996  \n",
       "77584    0.667393        2.006550  152.115044  39.235211  17.587312  \n",
       "\n",
       "[77585 rows x 18 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_adj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>gvkey</th>\n",
       "      <th>weights</th>\n",
       "      <th>predicted_return</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1078</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.044809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1230</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.019396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1246</td>\n",
       "      <td>0.01237</td>\n",
       "      <td>0.014087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1327</td>\n",
       "      <td>0.01888</td>\n",
       "      <td>0.128518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1602</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.045424</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  trade_date  gvkey  weights  predicted_return\n",
       "0           0  2001-03-01   1078  0.00000          0.044809\n",
       "1           1  2001-03-01   1230  0.00000          0.019396\n",
       "2           2  2001-03-01   1246  0.01237          0.014087\n",
       "3           3  2001-03-01   1327  0.01888          0.128518\n",
       "4           4  2001-03-01   1602  0.00000          0.045424"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weight_meanv.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>gvkey</th>\n",
       "      <th>weights</th>\n",
       "      <th>predicted_return</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1078</td>\n",
       "      <td>0.00662</td>\n",
       "      <td>0.044809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1230</td>\n",
       "      <td>0.00631</td>\n",
       "      <td>0.019396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1246</td>\n",
       "      <td>0.00629</td>\n",
       "      <td>0.014087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1327</td>\n",
       "      <td>0.00628</td>\n",
       "      <td>0.128518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1602</td>\n",
       "      <td>0.00628</td>\n",
       "      <td>0.045424</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  trade_date  gvkey  weights  predicted_return\n",
       "0           0  2001-03-01   1078  0.00662          0.044809\n",
       "1           1  2001-03-01   1230  0.00631          0.019396\n",
       "2           2  2001-03-01   1246  0.00629          0.014087\n",
       "3           3  2001-03-01   1327  0.00628          0.128518\n",
       "4           4  2001-03-01   1602  0.00628          0.045424"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weight_minv.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>gvkey</th>\n",
       "      <th>weights</th>\n",
       "      <th>predicted_return</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1078</td>\n",
       "      <td>0.006579</td>\n",
       "      <td>0.044809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1230</td>\n",
       "      <td>0.006579</td>\n",
       "      <td>0.019396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1246</td>\n",
       "      <td>0.006579</td>\n",
       "      <td>0.014087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1327</td>\n",
       "      <td>0.006579</td>\n",
       "      <td>0.128518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>2001-03-01</td>\n",
       "      <td>1602</td>\n",
       "      <td>0.006579</td>\n",
       "      <td>0.045424</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  trade_date  gvkey   weights  predicted_return\n",
       "0           0  2001-03-01   1078  0.006579          0.044809\n",
       "1           1  2001-03-01   1230  0.006579          0.019396\n",
       "2           2  2001-03-01   1246  0.006579          0.014087\n",
       "3           3  2001-03-01   1327  0.006579          0.128518\n",
       "4           4  2001-03-01   1602  0.006579          0.045424"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weight_equal.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16319 16319 16319\n"
     ]
    }
   ],
   "source": [
    "#the length of the 3 tables should be the same\n",
    "print(len(weight_meanv),\n",
    "len(weight_minv),\n",
    "len(weight_equal))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 Get stock name, trade date, daily date"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add 20170901 as tradedate_full\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#get unique stock name from table\n",
    "#stock names are the same between mean variance and minimum variance\n",
    "#only the weights are different\n",
    "stocks_name = list(set(weight_meanv['gvkey']))\n",
    "stocks_name.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "858"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(stocks_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'> <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "#get all daily date\n",
    "#convert to list from pandas.core.series.Series\n",
    "#use set to get unique values\n",
    "daily = list(set(data_adj['date']))\n",
    "#sort it\n",
    "daily.sort()\n",
    "print(type(data_adj['date']),type(daily))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "108"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(daily)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "#get unique trade date from table\n",
    "tradedate = list(set(weight_meanv['trade_date']))\n",
    "tradedate.sort()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2001-03-01',\n",
       " '2001-06-01',\n",
       " '2001-09-01',\n",
       " '2001-12-01',\n",
       " '2002-03-01',\n",
       " '2002-06-01',\n",
       " '2002-09-01',\n",
       " '2002-12-01',\n",
       " '2003-03-01',\n",
       " '2003-06-01',\n",
       " '2003-09-01',\n",
       " '2003-12-01',\n",
       " '2004-03-01',\n",
       " '2004-06-01',\n",
       " '2004-09-01',\n",
       " '2004-12-01',\n",
       " '2005-03-01',\n",
       " '2005-06-01',\n",
       " '2005-09-01',\n",
       " '2005-12-01',\n",
       " '2006-03-01',\n",
       " '2006-06-01',\n",
       " '2006-09-01',\n",
       " '2006-12-01',\n",
       " '2007-03-01',\n",
       " '2007-06-01',\n",
       " '2007-09-01',\n",
       " '2007-12-01',\n",
       " '2008-03-01',\n",
       " '2008-06-01',\n",
       " '2008-09-01',\n",
       " '2008-12-01',\n",
       " '2009-03-01',\n",
       " '2009-06-01',\n",
       " '2009-09-01',\n",
       " '2009-12-01',\n",
       " '2010-03-01',\n",
       " '2010-06-01',\n",
       " '2010-09-01',\n",
       " '2010-12-01',\n",
       " '2011-03-01',\n",
       " '2011-06-01',\n",
       " '2011-09-01',\n",
       " '2011-12-01',\n",
       " '2012-03-01',\n",
       " '2012-06-01',\n",
       " '2012-09-01',\n",
       " '2012-12-01',\n",
       " '2013-03-01',\n",
       " '2013-06-01',\n",
       " '2013-09-01',\n",
       " '2013-12-01',\n",
       " '2014-03-01',\n",
       " '2014-06-01',\n",
       " '2014-09-01',\n",
       " '2014-12-01',\n",
       " '2015-03-01',\n",
       " '2015-06-01',\n",
       " '2015-09-01',\n",
       " '2015-12-01',\n",
       " '2016-03-01',\n",
       " '2016-06-01',\n",
       " '2016-09-01',\n",
       " '2016-12-01',\n",
       " '2017-03-01',\n",
       " '2017-06-01',\n",
       " '2017-09-01',\n",
       " '2017-12-01',\n",
       " '2018-03-01',\n",
       " '2018-06-01',\n",
       " '2018-09-01',\n",
       " '2018-12-01',\n",
       " '2019-03-01',\n",
       " '2019-06-01',\n",
       " '2019-09-01',\n",
       " '2019-12-01',\n",
       " '2020-03-01',\n",
       " '2020-06-01',\n",
       " '2020-09-01',\n",
       " '2020-12-01',\n",
       " '2021-03-01',\n",
       " '2021-06-01',\n",
       " '2021-09-01',\n",
       " '2021-12-01',\n",
       " '2022-03-01',\n",
       " '2022-06-01',\n",
       " '2022-09-01',\n",
       " '2022-12-01']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tradedate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "tradedate_full = [x for x in tradedate if x > '2018-01-01']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2018-03-01',\n",
       " '2018-06-01',\n",
       " '2018-09-01',\n",
       " '2018-12-01',\n",
       " '2019-03-01',\n",
       " '2019-06-01',\n",
       " '2019-09-01',\n",
       " '2019-12-01',\n",
       " '2020-03-01',\n",
       " '2020-06-01',\n",
       " '2020-09-01',\n",
       " '2020-12-01',\n",
       " '2021-03-01',\n",
       " '2021-06-01',\n",
       " '2021-09-01',\n",
       " '2021-12-01',\n",
       " '2022-03-01',\n",
       " '2022-06-01',\n",
       " '2022-09-01',\n",
       " '2022-12-01']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tradedate_full"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 Get the daily price matrix based on stock_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14.190996885299683\n"
     ]
    }
   ],
   "source": [
    "#30mins running time\n",
    "##################match current all stocks to daily adjusted price table#######################\n",
    "# backtest daily price matrix:\n",
    "# covert original table to matrix\n",
    "# rows = all stocks: 971, columns = all dates:7155 days\n",
    "#\n",
    "start_time = time.time()\n",
    "\n",
    "balance_daily = pd.DataFrame(index = stocks_name, columns = daily)\n",
    "balance_daily = balance_daily.fillna(np.nan)\n",
    "\n",
    "for i in stocks_name:\n",
    "    ind = np.where(data_adj['gvkey'] == i)[0]\n",
    "    tmp = data_adj.loc[ind, :]\n",
    "    tmp.index = range(tmp.shape[0])\n",
    "    for j in daily:\n",
    "        ind2 = np.where(tmp['date'] == j)[0]\n",
    "        if(len(ind2) != 0):\n",
    "            balance_daily.loc[i,j] = tmp.loc[ind2[0], 'adj_close_q']\n",
    "\n",
    "elapsed_time = time.time() - start_time\n",
    "print(elapsed_time)\n",
    "writer2 = pd.ExcelWriter('balance_daily.xlsx')\n",
    "writer2 = pd.ExcelWriter('balance_daily_user8.xlsx')\n",
    "\n",
    "balance_daily.to_excel(writer2,'Sheet1')\n",
    "writer2.save()\n",
    "###############################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "balance_daily = pd.read_excel('balance_daily_user8.xlsx', index_col=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1996-03-01</th>\n",
       "      <th>1996-06-01</th>\n",
       "      <th>1996-09-01</th>\n",
       "      <th>1996-12-01</th>\n",
       "      <th>1997-03-01</th>\n",
       "      <th>1997-06-01</th>\n",
       "      <th>1997-09-01</th>\n",
       "      <th>1997-12-01</th>\n",
       "      <th>1998-03-01</th>\n",
       "      <th>1998-06-01</th>\n",
       "      <th>...</th>\n",
       "      <th>2020-09-01</th>\n",
       "      <th>2020-12-01</th>\n",
       "      <th>2021-03-01</th>\n",
       "      <th>2021-06-01</th>\n",
       "      <th>2021-09-01</th>\n",
       "      <th>2021-12-01</th>\n",
       "      <th>2022-03-01</th>\n",
       "      <th>2022-06-01</th>\n",
       "      <th>2022-09-01</th>\n",
       "      <th>2022-12-01</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1045</th>\n",
       "      <td>NaN</td>\n",
       "      <td>44.7500</td>\n",
       "      <td>45.5000</td>\n",
       "      <td>39.81250</td>\n",
       "      <td>44.0625</td>\n",
       "      <td>41.25000</td>\n",
       "      <td>46.25000</td>\n",
       "      <td>55.34350</td>\n",
       "      <td>64.2500</td>\n",
       "      <td>71.59350</td>\n",
       "      <td>...</td>\n",
       "      <td>13.07</td>\n",
       "      <td>12.29</td>\n",
       "      <td>15.77</td>\n",
       "      <td>23.90</td>\n",
       "      <td>21.21</td>\n",
       "      <td>20.52</td>\n",
       "      <td>17.96</td>\n",
       "      <td>18.25</td>\n",
       "      <td>12.68</td>\n",
       "      <td>12.04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1075</th>\n",
       "      <td>NaN</td>\n",
       "      <td>28.8750</td>\n",
       "      <td>30.3750</td>\n",
       "      <td>29.62500</td>\n",
       "      <td>31.7500</td>\n",
       "      <td>30.12500</td>\n",
       "      <td>30.06200</td>\n",
       "      <td>33.62500</td>\n",
       "      <td>42.3750</td>\n",
       "      <td>44.43700</td>\n",
       "      <td>...</td>\n",
       "      <td>73.29</td>\n",
       "      <td>74.55</td>\n",
       "      <td>79.95</td>\n",
       "      <td>81.35</td>\n",
       "      <td>81.97</td>\n",
       "      <td>72.36</td>\n",
       "      <td>70.59</td>\n",
       "      <td>78.10</td>\n",
       "      <td>73.12</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1078</th>\n",
       "      <td>NaN</td>\n",
       "      <td>20.3750</td>\n",
       "      <td>21.7500</td>\n",
       "      <td>24.62500</td>\n",
       "      <td>25.3750</td>\n",
       "      <td>28.06250</td>\n",
       "      <td>33.37500</td>\n",
       "      <td>31.96850</td>\n",
       "      <td>32.7500</td>\n",
       "      <td>37.65600</td>\n",
       "      <td>...</td>\n",
       "      <td>91.43</td>\n",
       "      <td>108.83</td>\n",
       "      <td>109.49</td>\n",
       "      <td>119.84</td>\n",
       "      <td>115.93</td>\n",
       "      <td>118.13</td>\n",
       "      <td>140.74</td>\n",
       "      <td>118.36</td>\n",
       "      <td>108.65</td>\n",
       "      <td>96.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1161</th>\n",
       "      <td>NaN</td>\n",
       "      <td>8.6875</td>\n",
       "      <td>6.8125</td>\n",
       "      <td>7.37500</td>\n",
       "      <td>12.8750</td>\n",
       "      <td>20.75000</td>\n",
       "      <td>17.93750</td>\n",
       "      <td>16.28100</td>\n",
       "      <td>8.8750</td>\n",
       "      <td>14.53100</td>\n",
       "      <td>...</td>\n",
       "      <td>52.61</td>\n",
       "      <td>81.99</td>\n",
       "      <td>91.71</td>\n",
       "      <td>78.50</td>\n",
       "      <td>93.93</td>\n",
       "      <td>102.90</td>\n",
       "      <td>143.90</td>\n",
       "      <td>109.34</td>\n",
       "      <td>76.47</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1177</th>\n",
       "      <td>NaN</td>\n",
       "      <td>18.8750</td>\n",
       "      <td>17.8750</td>\n",
       "      <td>17.59375</td>\n",
       "      <td>20.0000</td>\n",
       "      <td>21.46875</td>\n",
       "      <td>25.59375</td>\n",
       "      <td>20.35925</td>\n",
       "      <td>17.6405</td>\n",
       "      <td>20.85925</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>270281</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1.72</td>\n",
       "      <td>1.51</td>\n",
       "      <td>2.05</td>\n",
       "      <td>1.73</td>\n",
       "      <td>2.03</td>\n",
       "      <td>4.06</td>\n",
       "      <td>3.18</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>287882</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>11.13</td>\n",
       "      <td>9.84</td>\n",
       "      <td>12.91</td>\n",
       "      <td>12.82</td>\n",
       "      <td>14.62</td>\n",
       "      <td>14.35</td>\n",
       "      <td>12.89</td>\n",
       "      <td>18.60</td>\n",
       "      <td>10.86</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294524</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>65.72</td>\n",
       "      <td>70.49</td>\n",
       "      <td>91.66</td>\n",
       "      <td>104.05</td>\n",
       "      <td>102.87</td>\n",
       "      <td>93.85</td>\n",
       "      <td>92.23</td>\n",
       "      <td>102.82</td>\n",
       "      <td>87.46</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>312009</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>6.98</td>\n",
       "      <td>6.50</td>\n",
       "      <td>6.22</td>\n",
       "      <td>7.15</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.37</td>\n",
       "      <td>7.07</td>\n",
       "      <td>8.80</td>\n",
       "      <td>8.86</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>316056</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>102.22</td>\n",
       "      <td>98.91</td>\n",
       "      <td>116.38</td>\n",
       "      <td>125.62</td>\n",
       "      <td>139.30</td>\n",
       "      <td>132.18</td>\n",
       "      <td>132.44</td>\n",
       "      <td>109.78</td>\n",
       "      <td>97.90</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>858 rows × 108 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        1996-03-01  1996-06-01  1996-09-01  1996-12-01  1997-03-01  \\\n",
       "1045           NaN     44.7500     45.5000    39.81250     44.0625   \n",
       "1075           NaN     28.8750     30.3750    29.62500     31.7500   \n",
       "1078           NaN     20.3750     21.7500    24.62500     25.3750   \n",
       "1161           NaN      8.6875      6.8125     7.37500     12.8750   \n",
       "1177           NaN     18.8750     17.8750    17.59375     20.0000   \n",
       "...            ...         ...         ...         ...         ...   \n",
       "270281         NaN         NaN         NaN         NaN         NaN   \n",
       "287882         NaN         NaN         NaN         NaN         NaN   \n",
       "294524         NaN         NaN         NaN         NaN         NaN   \n",
       "312009         NaN         NaN         NaN         NaN         NaN   \n",
       "316056         NaN         NaN         NaN         NaN         NaN   \n",
       "\n",
       "        1997-06-01  1997-09-01  1997-12-01  1998-03-01  1998-06-01  ...  \\\n",
       "1045      41.25000    46.25000    55.34350     64.2500    71.59350  ...   \n",
       "1075      30.12500    30.06200    33.62500     42.3750    44.43700  ...   \n",
       "1078      28.06250    33.37500    31.96850     32.7500    37.65600  ...   \n",
       "1161      20.75000    17.93750    16.28100      8.8750    14.53100  ...   \n",
       "1177      21.46875    25.59375    20.35925     17.6405    20.85925  ...   \n",
       "...            ...         ...         ...         ...         ...  ...   \n",
       "270281         NaN         NaN         NaN         NaN         NaN  ...   \n",
       "287882         NaN         NaN         NaN         NaN         NaN  ...   \n",
       "294524         NaN         NaN         NaN         NaN         NaN  ...   \n",
       "312009         NaN         NaN         NaN         NaN         NaN  ...   \n",
       "316056         NaN         NaN         NaN         NaN         NaN  ...   \n",
       "\n",
       "        2020-09-01  2020-12-01  2021-03-01  2021-06-01  2021-09-01  \\\n",
       "1045         13.07       12.29       15.77       23.90       21.21   \n",
       "1075         73.29       74.55       79.95       81.35       81.97   \n",
       "1078         91.43      108.83      109.49      119.84      115.93   \n",
       "1161         52.61       81.99       91.71       78.50       93.93   \n",
       "1177           NaN         NaN         NaN         NaN         NaN   \n",
       "...            ...         ...         ...         ...         ...   \n",
       "270281        1.72        1.51        2.05        1.73        2.03   \n",
       "287882       11.13        9.84       12.91       12.82       14.62   \n",
       "294524       65.72       70.49       91.66      104.05      102.87   \n",
       "312009        6.98        6.50        6.22        7.15        9.00   \n",
       "316056      102.22       98.91      116.38      125.62      139.30   \n",
       "\n",
       "        2021-12-01  2022-03-01  2022-06-01  2022-09-01  2022-12-01  \n",
       "1045         20.52       17.96       18.25       12.68       12.04  \n",
       "1075         72.36       70.59       78.10       73.12         NaN  \n",
       "1078        118.13      140.74      118.36      108.65       96.76  \n",
       "1161        102.90      143.90      109.34       76.47         NaN  \n",
       "1177           NaN         NaN         NaN         NaN         NaN  \n",
       "...            ...         ...         ...         ...         ...  \n",
       "270281        4.06        3.18         NaN         NaN         NaN  \n",
       "287882       14.35       12.89       18.60       10.86         NaN  \n",
       "294524       93.85       92.23      102.82       87.46         NaN  \n",
       "312009        9.37        7.07        8.80        8.86         NaN  \n",
       "316056      132.18      132.44      109.78       97.90         NaN  \n",
       "\n",
       "[858 rows x 108 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balance_daily"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Function: calculate the rebalance matrix:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. shares table\n",
    "2. cost table\n",
    "3. cash table\n",
    "4. portfolio table\n",
    "5. portfolio_cost table\n",
    "6. portfolio_return table\n",
    "7. portfolio_cumsum table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# set initial capital to 1 million and transaction percet to 0.1%\n",
    "\n",
    "capital = 1000000\n",
    "transaction_percent = 1/1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(16319, 5)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weight_meanv.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#construct an empty matrix\n",
    "#rows are stocks: 971 unique stocks\n",
    "#columns are trade dates\n",
    "#name the index as stock names\n",
    "\n",
    "def cal_portfolio(stocks_name,tradedate, weight_table, capital, transaction_percent):\n",
    "\n",
    "\n",
    "    balance_weight = pd.DataFrame(index = stocks_name, columns = tradedate)\n",
    "    balance_weight = balance_weight.fillna(0)\n",
    "    \n",
    "    #loop through the weights table to fill the matrix\n",
    "    #need weight tabele\n",
    "    #0. weights table\n",
    "    for i in range(weight_table.shape[0]):\n",
    "        tics = weight_table.loc[i, 'gvkey']\n",
    "        date = weight_table.loc[i, 'trade_date']\n",
    "        w = weight_table.loc[i, 'weights']\n",
    "        balance_weight.loc[tics, date] = w\n",
    "        \n",
    "    #1. shares table\n",
    "    balance_share = pd.DataFrame(index = stocks_name, columns = tradedate)\n",
    "    balance_share['initial'] = 0\n",
    "    balance_share = balance_share.fillna(0)\n",
    "    #2. costs table\n",
    "    balance_cost = pd.DataFrame(index = stocks_name, columns = tradedate)\n",
    "    balance_cost = balance_cost.fillna(0)\n",
    "    #3. cash table\n",
    "    balance_cash = pd.DataFrame(index = stocks_name, columns = tradedate)\n",
    "    balance_cash = balance_cash.fillna(0)\n",
    "\n",
    "    #4. portfolio without transaction costs table \n",
    "    portfolio = pd.DataFrame(index = range(1), columns = tradedate_full)\n",
    "    portfolio.loc[0,tradedate[0]] = capital\n",
    "    \n",
    "    \n",
    "    # calculate portfolio value without cost\n",
    "    balance_price_full = []\n",
    "    balance_price_full = balance_daily[tradedate_full]\n",
    "    balance_price = balance_daily[tradedate]\n",
    "    balance_price.replace([0, np.inf, -np.inf], np.nan, inplace=True)\n",
    "    for j in range(len(tradedate) - 1):\n",
    "        i = tradedate[j]\n",
    "        #current capital\n",
    "        cap = portfolio.loc[0, i]\n",
    "        #\n",
    "        balance_cash[i] = cap * balance_weight[i]\n",
    "        balance_share[i] = list(np.array(balance_cash[i])\n",
    "                                      / np.array(balance_price[i]))\n",
    "        balance_share[i] = balance_share[i].fillna(0)\n",
    "        balance_share[i] = balance_share[i].astype(int)\n",
    "        #next quarter price * current share to calculate next quarter total account value\n",
    "        a = balance_price_full[tradedate_full[j+1]].fillna(0)\n",
    "        portfolio.loc[0, tradedate_full[j+1]] = np.dot(balance_share[i], a)\n",
    "\n",
    "    balance_cost = abs(np.diff(balance_share, axis = 1))\n",
    "    balance_cost = balance_cost * balance_price\n",
    "    balance_cost = np.sum(balance_cost, axis = 0) * transaction_percent\n",
    "    \n",
    "    #5. portfolio with transaction cost table\n",
    "    #calcualte portolio value with transaction cost\n",
    "    balance_cost.index = tradedate_full\n",
    "    portfolio_cost = []\n",
    "    portfolio_cost = portfolio.loc[0,] - balance_cost\n",
    "    portfolio_cost.index = tradedate_full\n",
    "    portfolio_cost_value = portfolio_cost.copy()\n",
    "    \n",
    "    #6. portfolio return P&L table\n",
    "    portfolio_cumsum = portfolio_cost_value.copy()\n",
    "    portfolio_cumsum.index = range(len(portfolio_cumsum))\n",
    "    tmp = portfolio_cumsum.shift(1)\n",
    "    tmp[0] = 0\n",
    "    tmp.index = tradedate_full\n",
    "\n",
    "    portfolio_cumsum = portfolio_cost_value- tmp\n",
    "    portfolio_cumsum.index = range(len(portfolio_cumsum))\n",
    "    tmp.index = range(len(tmp))\n",
    "    for i in range(len(portfolio_cumsum)):\n",
    "        if(tmp[i] != 0):\n",
    "            portfolio_cumsum[i] = portfolio_cumsum[i]/tmp[i]\n",
    "\n",
    "    portfolio_cumsum.index = range(len(portfolio_cumsum))\n",
    "    portfolio_cumsum[0] = 0\n",
    "    portfolio_return=portfolio_cumsum.copy()\n",
    "\n",
    "\n",
    "\n",
    "    # 7. portfolio accumulative return table\n",
    "    initial = 0\n",
    "    for i in range(portfolio_cumsum.shape[0]):\n",
    "        portfolio_cumsum[i] = initial + portfolio_cumsum[i]\n",
    "        initial = portfolio_cumsum[i]\n",
    "    \n",
    "    #1. shares table\n",
    "    #2. costs table\n",
    "    #3. cash table\n",
    "    #4. portfolio without transaction costs table \n",
    "    #5. portfolio with transaction cost table\n",
    "    #6. portfolio return P&L table\n",
    "    #7. portfolio accumulative return table\n",
    "\n",
    "    return(balance_share,\n",
    "           balance_cost,\n",
    "           balance_cash,\n",
    "           portfolio,\n",
    "           portfolio_cost,\n",
    "           portfolio_return,\n",
    "           portfolio_cumsum)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_3642/1845856610.py:41: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  balance_price.replace([0, np.inf, -np.inf], np.nan, inplace=True)\n"
     ]
    }
   ],
   "source": [
    "#calculate mean variance allocation portolio\n",
    "(balance_share_meanv,\n",
    " balance_cost_meanv,\n",
    " balance_cash_meanv,\n",
    " portfolio_meanv,\n",
    " portfolio_cost_meanv,\n",
    " portfolio_return_meanv,\n",
    " portfolio_cumsum_meanv)=cal_portfolio(stocks_name,tradedate_full, weight_meanv, capital, transaction_percent)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.6256100382905669"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(portfolio_return_meanv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_3642/1845856610.py:41: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  balance_price.replace([0, np.inf, -np.inf], np.nan, inplace=True)\n"
     ]
    }
   ],
   "source": [
    "#calculate minimum variance allocation portolio\n",
    "(balance_share_minv,\n",
    " balance_cost_minv,\n",
    " balance_cash_minv,\n",
    " portfolio_minv,\n",
    " portfolio_cost_minv,\n",
    " portfolio_return_minv,\n",
    " portfolio_cumsum_minv)=cal_portfolio(stocks_name,tradedate_full, weight_minv, capital, transaction_percent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_3642/1845856610.py:41: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  balance_price.replace([0, np.inf, -np.inf], np.nan, inplace=True)\n"
     ]
    }
   ],
   "source": [
    "#calculate equally weighted allocation portolio\n",
    "(balance_share_equal,\n",
    " balance_cost_equal,\n",
    " balance_cash_equal,\n",
    " portfolio_equal,\n",
    " portfolio_cost_equal,\n",
    " portfolio_return_equal,\n",
    " portfolio_cumsum_equal)=cal_portfolio(stocks_name,tradedate_full, weight_equal, capital, transaction_percent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.6039422883904094"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(portfolio_return_equal)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. Calculate SPX Portfolio Value, Return and Cumulative Return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "\n",
    "dates = pd.date_range('1999-09-01', '2022-09-01')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "spx = pd.read_csv(\"SPX.csv\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "spx.date = pd.to_datetime(spx.date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "spx = spx.set_index(\"date\").reindex(dates, method='ffill')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     2677.669922\n",
       "1     2734.620117\n",
       "2     2901.520020\n",
       "3     2760.169922\n",
       "4     2803.689941\n",
       "5     2752.060059\n",
       "6     2926.459961\n",
       "7     3140.979980\n",
       "8     2954.219971\n",
       "9     3055.729980\n",
       "10    3526.649902\n",
       "11    3662.449951\n",
       "12    3901.820068\n",
       "13    4202.040039\n",
       "14    4524.089844\n",
       "15    4513.040039\n",
       "16    4306.259766\n",
       "Name: close, dtype: float64"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "tradedate = tradedate[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "spx_daily = list(set(spx.index))\n",
    "spx_daily.sort()\n",
    "spx = spx['close']\n",
    "spx.index = spx.index.astype(str)\n",
    "spx = spx[tradedate]\n",
    "spx.index = range(len(spx))\n",
    "spx_return = (spx - spx.shift(1))/spx\n",
    "spx_return[0] = 0\n",
    "\n",
    "spx_return.index = tradedate\n",
    "spx_return.index = range(len(spx))\n",
    "spx_cumsum=spx_return.copy()\n",
    "\n",
    "spx_value = spx_return.copy()\n",
    "initial = 0\n",
    "for i in range(len(spx_cumsum)):\n",
    "    spx_cumsum[i] = initial + spx_cumsum[i]\n",
    "    initial = spx_cumsum[i]\n",
    "\n",
    "initial = 1000000\n",
    "for i in range(len(spx_value)):\n",
    "    spx_value[i] = initial * (1 + spx_value[i])\n",
    "    initial = spx_value[i]\n",
    "\n",
    "spx_cumsum.index = tradedate\n",
    "spx_value.index = tradedate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8455213218848242"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(spx_return)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. Plot the Portfolio Performance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'time_ind' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m/home/wenbiaolin/ML4StockRec_using_gvkey/fundamental_back_testing.ipynb Cell 42\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> <a href='vscode-notebook-cell://ssh-remote%2B10.26.1.22/home/wenbiaolin/ML4StockRec_using_gvkey/fundamental_back_testing.ipynb#X56sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a>\u001b[0m time_ind\u001b[39m.\u001b[39mhead()\n",
      "\u001b[0;31mNameError\u001b[0m: name 'time_ind' is not defined"
     ]
    }
   ],
   "source": [
    "time_ind.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.1 Plot Portfolio Value with Transaction Cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ind = tradedate_full[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": false
       },
       "data": [
        {
         "mode": "lines",
         "name": "Mean-Variance",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          998097.6373722198,
          1023339.4722018229,
          1004583.3065904576,
          1031019.6217769366,
          809909.7023460879,
          949091.4082848827,
          935672.7535135892,
          950925.9353386193,
          1082048.282080676,
          760624.137775737,
          958583.6574496945,
          1028353.2224276176,
          1252929.3533126737,
          1413438.401572646,
          1504081.908906326,
          1459661.9218582301,
          1514824.4380524335,
          1510132.2849140002,
          1162363.9356803,
          25787.75
         ]
        },
        {
         "mode": "lines",
         "name": "Equally Weighted",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          998949.8181730722,
          980399.1316273996,
          998854.0858749872,
          1024825.8053336296,
          854417.3357285586,
          980499.5171671169,
          973611.4804992174,
          958861.2789376328,
          1038140.6236854413,
          724188.7089781559,
          916285.3825595068,
          977104.7060194196,
          1187790.756345991,
          1338886.6557853844,
          1424632.4071524558,
          1381438.4756382285,
          1423259.0638506,
          1387696.934248,
          1136045.7860163334,
          111673.29999999999
         ]
        },
        {
         "mode": "lines",
         "name": "Min-Variance",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          998895.8909723532,
          992374.6853119333,
          1008796.3304465894,
          1033877.2725289491,
          864068.4192988311,
          995085.7233387289,
          976002.05972606,
          947259.1794897235,
          1032189.032513042,
          715572.3709715783,
          903789.2488963603,
          954037.5797818311,
          1161121.0697542438,
          1309443.9512316308,
          1392765.2173538767,
          1351914.1622707942,
          1391383.3055489,
          1364547.2558339997,
          1131390.3898183997,
          104749.20999999999
         ]
        },
        {
         "mode": "lines",
         "name": "SPX",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          1000000,
          1015420.4215210223,
          901577.66175118,
          906222.2329908399,
          900080.8861314631,
          845560.1663892802,
          706118.0063925646,
          721381.9911742795,
          639771.4711544869,
          721064.9863496653,
          752840.1612591005,
          796535.1595781911,
          855691.12879557,
          829154.9188806105,
          817691.3192282643,
          876346.3705444649,
          890131.5083541947,
          884067.531932604,
          898085.6429648924,
          928678.2863560262,
          947787.7721022192,
          943711.196275488,
          961923.0628269009,
          1020945.1660701773,
          1025645.5162149781,
          1114548.451961276,
          1067402.9019714282,
          1072555.6604772124,
          951236.9149655188,
          998616.0367663883,
          907109.4708895985,
          388523.13879740227,
          345648.1248933483,
          421818.49492525955,
          445135.91674057767,
          489623.01719070674,
          492629.09030244197,
          471924.78206550394,
          476109.8398905623,
          525762.8900103732,
          566114.8640599039,
          569654.8713096414,
          517566.6533542639,
          534267.4134848869,
          584622.9066905852,
          540686.108427394,
          590096.5576499363,
          594096.7443247958,
          634018.8170999325,
          677773.4946512762,
          678699.0607562413,
          743659.6056120741,
          765112.0901678336,
          790616.223033034,
          822108.7647782513,
          842154.6111973216,
          862587.2383262035,
          865540.4988961497,
          776049.0756745328,
          845724.9010333614,
          792596.479097556,
          838272.1945614931,
          865893.3362604625,
          873884.070175674,
          948610.3881477184,
          961921.8726763312,
          979979.1449241162,
          1041424.8497400946,
          1055212.3818667624,
          1077187.8478065385,
          1139149.3526021626,
          1080812.773046224,
          1097589.590018005,
          1076998.3163167306,
          1141181.1186434878,
          1219120.5507259427,
          1142050.1334188452,
          1179988.5379693666,
          1337554.5355878528,
          1387149.7447141893,
          1472249.0550697441,
          1577435.7177554606,
          1689726.3402575864,
          1685589.1852858684,
          1604649.6666300267,
          1524429.591684949,
          1468066.175902882
         ]
        }
       ],
       "layout": {
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        },
        "title": {
         "text": "Portfolio Value Performance Quarterly with Transaction cost"
        },
        "xaxis": {
         "title": {
          "text": "Quarterly Trade Date"
         }
        },
        "yaxis": {
         "title": {
          "text": "Portfolio Value"
         }
        }
       }
      },
      "text/html": [
       "<div>                            <div id=\"cdda9b4c-b55d-48ae-b915-a55bcf9750e7\" class=\"plotly-graph-div\" style=\"height:525px; width:100%;\"></div>            <script type=\"text/javascript\">                require([\"plotly\"], function(Plotly) {                    window.PLOTLYENV=window.PLOTLYENV || {};                                    if (document.getElementById(\"cdda9b4c-b55d-48ae-b915-a55bcf9750e7\")) {                    Plotly.newPlot(                        \"cdda9b4c-b55d-48ae-b915-a55bcf9750e7\",                        [{\"mode\":\"lines\",\"name\":\"Mean-Variance\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[998097.6373722198,1023339.4722018229,1004583.3065904576,1031019.6217769366,809909.7023460879,949091.4082848827,935672.7535135892,950925.9353386193,1082048.282080676,760624.137775737,958583.6574496945,1028353.2224276176,1252929.3533126737,1413438.401572646,1504081.908906326,1459661.9218582301,1514824.4380524335,1510132.2849140002,1162363.9356803,25787.75],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"Equally Weighted\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[998949.8181730722,980399.1316273996,998854.0858749872,1024825.8053336296,854417.3357285586,980499.5171671169,973611.4804992174,958861.2789376328,1038140.6236854413,724188.7089781559,916285.3825595068,977104.7060194196,1187790.756345991,1338886.6557853844,1424632.4071524558,1381438.4756382285,1423259.0638506,1387696.934248,1136045.7860163334,111673.29999999999],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"Min-Variance\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[998895.8909723532,992374.6853119333,1008796.3304465894,1033877.2725289491,864068.4192988311,995085.7233387289,976002.05972606,947259.1794897235,1032189.032513042,715572.3709715783,903789.2488963603,954037.5797818311,1161121.0697542438,1309443.9512316308,1392765.2173538767,1351914.1622707942,1391383.3055489,1364547.2558339997,1131390.3898183997,104749.20999999999],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"SPX\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[1000000.0,1015420.4215210223,901577.66175118,906222.2329908399,900080.8861314631,845560.1663892802,706118.0063925646,721381.9911742795,639771.4711544869,721064.9863496653,752840.1612591005,796535.1595781911,855691.12879557,829154.9188806105,817691.3192282643,876346.3705444649,890131.5083541947,884067.531932604,898085.6429648924,928678.2863560262,947787.7721022192,943711.196275488,961923.0628269009,1020945.1660701773,1025645.5162149781,1114548.451961276,1067402.9019714282,1072555.6604772124,951236.9149655188,998616.0367663883,907109.4708895985,388523.13879740227,345648.1248933483,421818.49492525955,445135.91674057767,489623.01719070674,492629.09030244197,471924.78206550394,476109.8398905623,525762.8900103732,566114.8640599039,569654.8713096414,517566.6533542639,534267.4134848869,584622.9066905852,540686.108427394,590096.5576499363,594096.7443247958,634018.8170999325,677773.4946512762,678699.0607562413,743659.6056120741,765112.0901678336,790616.223033034,822108.7647782513,842154.6111973216,862587.2383262035,865540.4988961497,776049.0756745328,845724.9010333614,792596.479097556,838272.1945614931,865893.3362604625,873884.070175674,948610.3881477184,961921.8726763312,979979.1449241162,1041424.8497400946,1055212.3818667624,1077187.8478065385,1139149.3526021626,1080812.773046224,1097589.590018005,1076998.3163167306,1141181.1186434878,1219120.5507259427,1142050.1334188452,1179988.5379693666,1337554.5355878528,1387149.7447141893,1472249.0550697441,1577435.7177554606,1689726.3402575864,1685589.1852858684,1604649.6666300267,1524429.591684949,1468066.175902882],\"type\":\"scatter\"}],                        {\"template\":{\"data\":{\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"bar\":[{\"error_x\":{\"color\":\"#2a3f5f\"},\"error_y\":{\"color\":\"#2a3f5f\"},\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"baxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"type\":\"carpet\"}],\"choropleth\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"choropleth\"}],\"contourcarpet\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"contourcarpet\"}],\"contour\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"contour\"}],\"heatmapgl\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"heatmapgl\"}],\"heatmap\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"heatmap\"}],\"histogram2dcontour\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"histogram2dcontour\"}],\"histogram2d\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"histogram2d\"}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"mesh3d\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"mesh3d\"}],\"parcoords\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"parcoords\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}],\"scatter3d\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatter3d\"}],\"scattercarpet\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattercarpet\"}],\"scattergeo\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattergeo\"}],\"scattergl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattergl\"}],\"scattermapbox\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattermapbox\"}],\"scatterpolargl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatterpolargl\"}],\"scatterpolar\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatterpolar\"}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"scatterternary\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatterternary\"}],\"surface\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"surface\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"#EBF0F8\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"#C8D4E3\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}]},\"layout\":{\"annotationdefaults\":{\"arrowcolor\":\"#2a3f5f\",\"arrowhead\":0,\"arrowwidth\":1},\"autotypenumbers\":\"strict\",\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"colorscale\":{\"diverging\":[[0,\"#8e0152\"],[0.1,\"#c51b7d\"],[0.2,\"#de77ae\"],[0.3,\"#f1b6da\"],[0.4,\"#fde0ef\"],[0.5,\"#f7f7f7\"],[0.6,\"#e6f5d0\"],[0.7,\"#b8e186\"],[0.8,\"#7fbc41\"],[0.9,\"#4d9221\"],[1,\"#276419\"]],\"sequential\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"sequentialminus\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]},\"colorway\":[\"#636efa\",\"#EF553B\",\"#00cc96\",\"#ab63fa\",\"#FFA15A\",\"#19d3f3\",\"#FF6692\",\"#B6E880\",\"#FF97FF\",\"#FECB52\"],\"font\":{\"color\":\"#2a3f5f\"},\"geo\":{\"bgcolor\":\"white\",\"lakecolor\":\"white\",\"landcolor\":\"#E5ECF6\",\"showlakes\":true,\"showland\":true,\"subunitcolor\":\"white\"},\"hoverlabel\":{\"align\":\"left\"},\"hovermode\":\"closest\",\"mapbox\":{\"style\":\"light\"},\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"#E5ECF6\",\"polar\":{\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"bgcolor\":\"#E5ECF6\",\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\"},\"yaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\"},\"zaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\"}},\"shapedefaults\":{\"line\":{\"color\":\"#2a3f5f\"}},\"ternary\":{\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"bgcolor\":\"#E5ECF6\",\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"title\":{\"x\":0.05},\"xaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"zerolinewidth\":2},\"yaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"zerolinewidth\":2}}},\"title\":{\"text\":\"Portfolio Value Performance Quarterly with Transaction cost\"},\"xaxis\":{\"title\":{\"text\":\"Quarterly Trade Date\"}},\"yaxis\":{\"title\":{\"text\":\"Portfolio Value\"}}},                        {\"responsive\": true}                    ).then(function(){\n",
       "                            \n",
       "var gd = document.getElementById('cdda9b4c-b55d-48ae-b915-a55bcf9750e7');\n",
       "var x = new MutationObserver(function (mutations, observer) {{\n",
       "        var display = window.getComputedStyle(gd).display;\n",
       "        if (!display || display === 'none') {{\n",
       "            console.log([gd, 'removed!']);\n",
       "            Plotly.purge(gd);\n",
       "            observer.disconnect();\n",
       "        }}\n",
       "}});\n",
       "\n",
       "// Listen for the removal of the full notebook cells\n",
       "var notebookContainer = gd.closest('#notebook-container');\n",
       "if (notebookContainer) {{\n",
       "    x.observe(notebookContainer, {childList: true});\n",
       "}}\n",
       "\n",
       "// Listen for the clearing of the current output cell\n",
       "var outputEl = gd.closest('.output');\n",
       "if (outputEl) {{\n",
       "    x.observe(outputEl, {childList: true});\n",
       "}}\n",
       "\n",
       "                        })                };                });            </script>        </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "trace0 = go.Scatter(x = time_ind, y = portfolio_cost_meanv, mode = 'lines', name = 'Mean-Variance')\n",
    "trace1 = go.Scatter(x = time_ind, y = portfolio_cost_equal, mode = 'lines', name = 'Equally Weighted')\n",
    "trace2 = go.Scatter(x = time_ind, y = portfolio_cost_minv, mode = 'lines', name = 'Min-Variance')\n",
    "trace3 = go.Scatter(x = time_ind, y = spx_value, mode = 'lines', name = 'SPX')\n",
    "\n",
    "data = [trace0, trace1, trace2, trace3]\n",
    "layout = dict(title = \"Portfolio Value Performance Quarterly with Transaction cost\", \n",
    "              xaxis = dict(title = 'Quarterly Trade Date'), \n",
    "              yaxis = dict(title = 'Portfolio Value'))\n",
    "fig = dict(data = data, layout = layout)\n",
    "\n",
    "plotly.offline.iplot(fig)\n",
    "\n",
    "#portfolio_tmp = portfolio_cost_value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.2 Plot Portfolio Return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": false
       },
       "data": [
        {
         "mode": "lines",
         "name": "Mean-Variance",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          0,
          0.02528994547673663,
          -0.01832839064734743,
          0.026315702254901603,
          -0.21445752802431756,
          0.17184842401026096,
          -0.014138421920331748,
          0.016301833913355053,
          0.13788912665986414,
          -0.29705157304706487,
          0.2602593184234761,
          0.07278401257491145,
          0.2183842341203567,
          0.1281070220245025,
          0.06412978961999784,
          -0.029532957470644147,
          0.0377912963050913,
          -0.0030974897292163554,
          -0.23028999029280742,
          -0.9778143925422917
         ]
        },
        {
         "mode": "lines",
         "name": "Minimum-Variance",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          0,
          -0.006528413740967504,
          0.01654782752695298,
          0.024862245554815286,
          -0.16424469106932924,
          0.1516283908931828,
          -0.01917790916408591,
          -0.029449610223572602,
          0.08965851676314089,
          -0.30674290422424455,
          0.26302982837253475,
          0.05559739833908219,
          0.21706009738082693,
          0.12774109896117902,
          0.06363102906686155,
          -0.02933089839843624,
          0.029195006886983142,
          -0.019287316160742286,
          -0.17086756432857764,
          -0.9074155031343218
         ]
        },
        {
         "mode": "lines",
         "name": "Equally Weighted",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          0,
          -0.01857018862028424,
          0.0188239194142834,
          0.026001514961909,
          -0.16628042416398262,
          0.147565102165265,
          -0.007025028107918447,
          -0.015149987296802895,
          0.08268072398923629,
          -0.30241752181197135,
          0.26525775837129945,
          0.06637596170095289,
          0.21562279766809775,
          0.1272075057261859,
          0.06404257671593072,
          -0.030319352063991716,
          0.030273218062100333,
          -0.024986406555098595,
          -0.181344457871875,
          -0.9017000006737453
         ]
        },
        {
         "mode": "lines",
         "name": "SPX",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          0,
          0.015420421521022359,
          -0.11211391592786224,
          0.005151604167563888,
          -0.006776866242961527,
          -0.06057313357304172,
          -0.16491098509543442,
          0.021616761849334407,
          -0.1131307975777791,
          0.1270664899272264,
          0.04406700576364758,
          0.0580402063646712,
          0.07426661397935691,
          -0.031011435110132206,
          -0.013825642701152312,
          0.0717325107126723,
          0.015730238947832015,
          -0.006812450030897837,
          0.01585638033968322,
          0.03406428287856472,
          0.020577078227138664,
          -0.004301148365407913,
          0.019298135513586202,
          0.06135844489456598,
          0.004603920270168108,
          0.0866799828408391,
          -0.04230013500703855,
          0.004827379142652915,
          -0.11311184116795885,
          0.04980790910809731,
          -0.09163338310998546,
          -0.5716910127546356,
          -0.11035382355029158,
          0.22036968971092805,
          0.055278329650883605,
          0.09994048733671584,
          0.006139566577125166,
          -0.042028188437323,
          0.008868061148942749,
          0.10428906516870991,
          0.07674937660346187,
          0.006253160753191119,
          -0.0914382033381479,
          0.032267844194343066,
          0.09425147769586494,
          -0.0751540826751167,
          0.09138472110233163,
          0.006778868005585903,
          0.06719793225008994,
          0.06901163872624808,
          0.0013655979649091856,
          0.09571332658608728,
          0.02884718276193436,
          0.03333385159239327,
          0.039832906064591965,
          0.02438344812499034,
          0.024262322924091233,
          0.003423723930435997,
          -0.1033936867607559,
          0.08978275671324937,
          -0.06281998067088922,
          0.05762795655608152,
          0.032950086950478095,
          0.009228312057142194,
          0.08551056200969813,
          0.014032615175767864,
          0.01877207781703161,
          0.06270103311304294,
          0.013239104223515227,
          0.020825633130744352,
          0.057521540854546034,
          -0.051210650669010274,
          0.015522408126706626,
          -0.01876044915926794,
          0.0595941528917691,
          0.06829716230768071,
          -0.06321804456598228,
          0.033219561418857214,
          0.13353180352890567,
          0.0370790183179631,
          0.06134832283236208,
          0.07144624228047713,
          0.07118554578053078,
          -0.0024484171626794143,
          -0.04801853225115146,
          -0.04999226723023616,
          -0.036973446389064475
         ]
        }
       ],
       "layout": {
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        },
        "title": {
         "text": "Portfolio Return Quarterly"
        },
        "xaxis": {
         "title": {
          "text": "Quarterly Trade Date"
         }
        },
        "yaxis": {
         "title": {
          "text": "Portfolio Value"
         }
        }
       }
      },
      "text/html": [
       "<div>                            <div id=\"e06030b9-d0b9-4609-9858-63a433d9da3d\" class=\"plotly-graph-div\" style=\"height:525px; width:100%;\"></div>            <script type=\"text/javascript\">                require([\"plotly\"], function(Plotly) {                    window.PLOTLYENV=window.PLOTLYENV || {};                                    if (document.getElementById(\"e06030b9-d0b9-4609-9858-63a433d9da3d\")) {                    Plotly.newPlot(                        \"e06030b9-d0b9-4609-9858-63a433d9da3d\",                        [{\"mode\":\"lines\",\"name\":\"Mean-Variance\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[0,0.02528994547673663,-0.01832839064734743,0.026315702254901603,-0.21445752802431756,0.17184842401026096,-0.014138421920331748,0.016301833913355053,0.13788912665986414,-0.29705157304706487,0.2602593184234761,0.07278401257491145,0.2183842341203567,0.1281070220245025,0.06412978961999784,-0.029532957470644147,0.0377912963050913,-0.0030974897292163554,-0.23028999029280742,-0.9778143925422917],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"Minimum-Variance\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[0,-0.006528413740967504,0.01654782752695298,0.024862245554815286,-0.16424469106932924,0.1516283908931828,-0.01917790916408591,-0.029449610223572602,0.08965851676314089,-0.30674290422424455,0.26302982837253475,0.05559739833908219,0.21706009738082693,0.12774109896117902,0.06363102906686155,-0.02933089839843624,0.029195006886983142,-0.019287316160742286,-0.17086756432857764,-0.9074155031343218],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"Equally Weighted\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[0,-0.01857018862028424,0.0188239194142834,0.026001514961909,-0.16628042416398262,0.147565102165265,-0.007025028107918447,-0.015149987296802895,0.08268072398923629,-0.30241752181197135,0.26525775837129945,0.06637596170095289,0.21562279766809775,0.1272075057261859,0.06404257671593072,-0.030319352063991716,0.030273218062100333,-0.024986406555098595,-0.181344457871875,-0.9017000006737453],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"SPX\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[0.0,0.015420421521022359,-0.11211391592786224,0.005151604167563888,-0.006776866242961527,-0.06057313357304172,-0.16491098509543442,0.021616761849334407,-0.1131307975777791,0.1270664899272264,0.04406700576364758,0.0580402063646712,0.07426661397935691,-0.031011435110132206,-0.013825642701152312,0.0717325107126723,0.015730238947832015,-0.006812450030897837,0.01585638033968322,0.03406428287856472,0.020577078227138664,-0.004301148365407913,0.019298135513586202,0.06135844489456598,0.004603920270168108,0.0866799828408391,-0.04230013500703855,0.004827379142652915,-0.11311184116795885,0.04980790910809731,-0.09163338310998546,-0.5716910127546356,-0.11035382355029158,0.22036968971092805,0.055278329650883605,0.09994048733671584,0.006139566577125166,-0.042028188437323,0.008868061148942749,0.10428906516870991,0.07674937660346187,0.006253160753191119,-0.0914382033381479,0.032267844194343066,0.09425147769586494,-0.0751540826751167,0.09138472110233163,0.006778868005585903,0.06719793225008994,0.06901163872624808,0.0013655979649091856,0.09571332658608728,0.02884718276193436,0.03333385159239327,0.039832906064591965,0.02438344812499034,0.024262322924091233,0.003423723930435997,-0.1033936867607559,0.08978275671324937,-0.06281998067088922,0.05762795655608152,0.032950086950478095,0.009228312057142194,0.08551056200969813,0.014032615175767864,0.01877207781703161,0.06270103311304294,0.013239104223515227,0.020825633130744352,0.057521540854546034,-0.051210650669010274,0.015522408126706626,-0.01876044915926794,0.0595941528917691,0.06829716230768071,-0.06321804456598228,0.033219561418857214,0.13353180352890567,0.0370790183179631,0.06134832283236208,0.07144624228047713,0.07118554578053078,-0.0024484171626794143,-0.04801853225115146,-0.04999226723023616,-0.036973446389064475],\"type\":\"scatter\"}],                        {\"template\":{\"data\":{\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"bar\":[{\"error_x\":{\"color\":\"#2a3f5f\"},\"error_y\":{\"color\":\"#2a3f5f\"},\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"baxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"type\":\"carpet\"}],\"choropleth\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"choropleth\"}],\"contourcarpet\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"contourcarpet\"}],\"contour\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"contour\"}],\"heatmapgl\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"heatmapgl\"}],\"heatmap\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"heatmap\"}],\"histogram2dcontour\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"histogram2dcontour\"}],\"histogram2d\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"histogram2d\"}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"mesh3d\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"mesh3d\"}],\"parcoords\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"parcoords\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}],\"scatter3d\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatter3d\"}],\"scattercarpet\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattercarpet\"}],\"scattergeo\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattergeo\"}],\"scattergl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattergl\"}],\"scattermapbox\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattermapbox\"}],\"scatterpolargl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatterpolargl\"}],\"scatterpolar\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatterpolar\"}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"scatterternary\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatterternary\"}],\"surface\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"surface\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"#EBF0F8\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"#C8D4E3\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}]},\"layout\":{\"annotationdefaults\":{\"arrowcolor\":\"#2a3f5f\",\"arrowhead\":0,\"arrowwidth\":1},\"autotypenumbers\":\"strict\",\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"colorscale\":{\"diverging\":[[0,\"#8e0152\"],[0.1,\"#c51b7d\"],[0.2,\"#de77ae\"],[0.3,\"#f1b6da\"],[0.4,\"#fde0ef\"],[0.5,\"#f7f7f7\"],[0.6,\"#e6f5d0\"],[0.7,\"#b8e186\"],[0.8,\"#7fbc41\"],[0.9,\"#4d9221\"],[1,\"#276419\"]],\"sequential\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"sequentialminus\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]},\"colorway\":[\"#636efa\",\"#EF553B\",\"#00cc96\",\"#ab63fa\",\"#FFA15A\",\"#19d3f3\",\"#FF6692\",\"#B6E880\",\"#FF97FF\",\"#FECB52\"],\"font\":{\"color\":\"#2a3f5f\"},\"geo\":{\"bgcolor\":\"white\",\"lakecolor\":\"white\",\"landcolor\":\"#E5ECF6\",\"showlakes\":true,\"showland\":true,\"subunitcolor\":\"white\"},\"hoverlabel\":{\"align\":\"left\"},\"hovermode\":\"closest\",\"mapbox\":{\"style\":\"light\"},\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"#E5ECF6\",\"polar\":{\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"bgcolor\":\"#E5ECF6\",\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\"},\"yaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\"},\"zaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\"}},\"shapedefaults\":{\"line\":{\"color\":\"#2a3f5f\"}},\"ternary\":{\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"bgcolor\":\"#E5ECF6\",\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"title\":{\"x\":0.05},\"xaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"zerolinewidth\":2},\"yaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"zerolinewidth\":2}}},\"title\":{\"text\":\"Portfolio Return Quarterly\"},\"xaxis\":{\"title\":{\"text\":\"Quarterly Trade Date\"}},\"yaxis\":{\"title\":{\"text\":\"Portfolio Value\"}}},                        {\"responsive\": true}                    ).then(function(){\n",
       "                            \n",
       "var gd = document.getElementById('e06030b9-d0b9-4609-9858-63a433d9da3d');\n",
       "var x = new MutationObserver(function (mutations, observer) {{\n",
       "        var display = window.getComputedStyle(gd).display;\n",
       "        if (!display || display === 'none') {{\n",
       "            console.log([gd, 'removed!']);\n",
       "            Plotly.purge(gd);\n",
       "            observer.disconnect();\n",
       "        }}\n",
       "}});\n",
       "\n",
       "// Listen for the removal of the full notebook cells\n",
       "var notebookContainer = gd.closest('#notebook-container');\n",
       "if (notebookContainer) {{\n",
       "    x.observe(notebookContainer, {childList: true});\n",
       "}}\n",
       "\n",
       "// Listen for the clearing of the current output cell\n",
       "var outputEl = gd.closest('.output');\n",
       "if (outputEl) {{\n",
       "    x.observe(outputEl, {childList: true});\n",
       "}}\n",
       "\n",
       "                        })                };                });            </script>        </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "trace_return1 = go.Scatter(x = time_ind, y = portfolio_return_meanv, mode = 'lines', name = 'Mean-Variance')\n",
    "trace_return2 = go.Scatter(x = time_ind, y = portfolio_return_minv, mode = 'lines', name = 'Minimum-Variance')\n",
    "trace_return3 = go.Scatter(x = time_ind, y = portfolio_return_equal, mode = 'lines', name = 'Equally Weighted')\n",
    "trace_return4 = go.Scatter(x = time_ind, y = spx_return, mode = 'lines', name = 'SPX')\n",
    "\n",
    "data = [trace_return1,trace_return2,trace_return3, trace_return4]\n",
    "layout = dict(title = \"Portfolio Return Quarterly\", \n",
    "              xaxis = dict(title = 'Quarterly Trade Date'), \n",
    "              yaxis = dict(title = 'Portfolio Value'))\n",
    "fig = dict(data = data, layout = layout)\n",
    "plotly.offline.iplot(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.3 Plot Portfolio Cumulative Return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": false
       },
       "data": [
        {
         "mode": "lines",
         "name": "Mean-Variance",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          0,
          0.02528994547673663,
          0.006961554829389201,
          0.0332772570842908,
          -0.18118027094002676,
          -0.009331846929765797,
          -0.023470268850097543,
          -0.0071684349367424904,
          0.13072069172312165,
          -0.16633088132394322,
          0.09392843709953289,
          0.16671244967444432,
          0.385096683794801,
          0.5132037058193035,
          0.5773334954393013,
          0.5478005379686572,
          0.5855918342737485,
          0.5824943445445322,
          0.3522043542517248,
          -0.6256100382905669
         ]
        },
        {
         "mode": "lines",
         "name": "Minimum-Variance",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          0,
          -0.006528413740967504,
          0.010019413785985476,
          0.03488165934080076,
          -0.12936303172852848,
          0.02226535916465433,
          0.003087450000568419,
          -0.026362160223004184,
          0.06329635654013671,
          -0.24344654768410784,
          0.019583280688426907,
          0.0751806790275091,
          0.292240776408336,
          0.41998187536951503,
          0.4836129044363766,
          0.4542820060379404,
          0.4834770129249235,
          0.4641896967641812,
          0.29332213243560357,
          -0.6140933706987183
         ]
        },
        {
         "mode": "lines",
         "name": "Equally Weighted",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          0,
          -0.01857018862028424,
          0.0002537307939991622,
          0.026255245755908162,
          -0.14002517840807446,
          0.007539923757190536,
          0.0005148956492720897,
          -0.014635091647530806,
          0.06804563234170548,
          -0.23437188947026588,
          0.030885868901033564,
          0.09726183060198645,
          0.31288462827008423,
          0.4400921339962701,
          0.5041347107122008,
          0.4738153586482091,
          0.5040885767103095,
          0.4791021701552109,
          0.2977577122833359,
          -0.6039422883904094
         ]
        },
        {
         "mode": "lines",
         "name": "SPX",
         "type": "scatter",
         "x": [
          "2018-03-01",
          "2018-06-01",
          "2018-09-01",
          "2018-12-01",
          "2019-03-01",
          "2019-06-01",
          "2019-09-01",
          "2019-12-01",
          "2020-03-01",
          "2020-06-01",
          "2020-09-01",
          "2020-12-01",
          "2021-03-01",
          "2021-06-01",
          "2021-09-01",
          "2021-12-01",
          "2022-03-01",
          "2022-06-01",
          "2022-09-01"
         ],
         "y": [
          0,
          0.015420421521022359,
          -0.09669349440683989,
          -0.091541890239276,
          -0.09831875648223753,
          -0.15889189005527926,
          -0.3238028751507137,
          -0.3021861133013793,
          -0.4153169108791584,
          -0.288250420951932,
          -0.2441834151882844,
          -0.1861432088236132,
          -0.1118765948442563,
          -0.1428880299543885,
          -0.15671367265554081,
          -0.08498116194286852,
          -0.0692509229950365,
          -0.07606337302593434,
          -0.060206992686251126,
          -0.026142709807686404,
          -0.00556563158054774,
          -0.009866779945955653,
          0.009431355567630549,
          0.07078980046219653,
          0.07539372073236464,
          0.16207370357320372,
          0.11977356856616517,
          0.12460094770881808,
          0.011489106540859231,
          0.061297015648956545,
          -0.03033636746102892,
          -0.6020273802156646,
          -0.7123812037659562,
          -0.4920115140550281,
          -0.4367331844041445,
          -0.33679269706742865,
          -0.3306531304903035,
          -0.37268131892762646,
          -0.36381325777868373,
          -0.25952419260997384,
          -0.18277481600651196,
          -0.17652165525332084,
          -0.26795985859146876,
          -0.2356920143971257,
          -0.14144053670126078,
          -0.21659461937637747,
          -0.12520989827404583,
          -0.11843103026845993,
          -0.05123309801836999,
          0.017778540707878085,
          0.019144138672787272,
          0.11485746525887455,
          0.1437046480208089,
          0.17703849961320217,
          0.21687140567779414,
          0.2412548538027845,
          0.26551717672687575,
          0.26894090065731174,
          0.16554721389655586,
          0.2553299706098052,
          0.192509989938916,
          0.25013794649499754,
          0.2830880334454756,
          0.2923163455026178,
          0.37782690751231596,
          0.39185952268808383,
          0.41063160050511543,
          0.47333263361815836,
          0.48657173784167357,
          0.5073973709724179,
          0.564918911826964,
          0.5137082611579536,
          0.5292306692846602,
          0.5104702201253923,
          0.5700643730171614,
          0.6383615353248421,
          0.5751434907588598,
          0.608363052177717,
          0.7418948557066227,
          0.7789738740245858,
          0.8403221968569479,
          0.911768439137425,
          0.9829539849179558,
          0.9805055677552764,
          0.9324870355041249,
          0.8824947682738887,
          0.8455213218848242
         ]
        }
       ],
       "layout": {
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "heatmapgl": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmapgl"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        },
        "title": {
         "text": "Portfolio Cumulative Return Quarterly"
        },
        "xaxis": {
         "title": {
          "text": "Quarterly Trade Date"
         }
        },
        "yaxis": {
         "title": {
          "text": "Portfolio Value"
         }
        }
       }
      },
      "text/html": [
       "<div>                            <div id=\"9038a2f7-852f-4999-a7a4-655b2dee4242\" class=\"plotly-graph-div\" style=\"height:525px; width:100%;\"></div>            <script type=\"text/javascript\">                require([\"plotly\"], function(Plotly) {                    window.PLOTLYENV=window.PLOTLYENV || {};                                    if (document.getElementById(\"9038a2f7-852f-4999-a7a4-655b2dee4242\")) {                    Plotly.newPlot(                        \"9038a2f7-852f-4999-a7a4-655b2dee4242\",                        [{\"mode\":\"lines\",\"name\":\"Mean-Variance\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[0,0.02528994547673663,0.006961554829389201,0.0332772570842908,-0.18118027094002676,-0.009331846929765797,-0.023470268850097543,-0.0071684349367424904,0.13072069172312165,-0.16633088132394322,0.09392843709953289,0.16671244967444432,0.385096683794801,0.5132037058193035,0.5773334954393013,0.5478005379686572,0.5855918342737485,0.5824943445445322,0.3522043542517248,-0.6256100382905669],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"Minimum-Variance\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[0,-0.006528413740967504,0.010019413785985476,0.03488165934080076,-0.12936303172852848,0.02226535916465433,0.003087450000568419,-0.026362160223004184,0.06329635654013671,-0.24344654768410784,0.019583280688426907,0.0751806790275091,0.292240776408336,0.41998187536951503,0.4836129044363766,0.4542820060379404,0.4834770129249235,0.4641896967641812,0.29332213243560357,-0.6140933706987183],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"Equally Weighted\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[0,-0.01857018862028424,0.0002537307939991622,0.026255245755908162,-0.14002517840807446,0.007539923757190536,0.0005148956492720897,-0.014635091647530806,0.06804563234170548,-0.23437188947026588,0.030885868901033564,0.09726183060198645,0.31288462827008423,0.4400921339962701,0.5041347107122008,0.4738153586482091,0.5040885767103095,0.4791021701552109,0.2977577122833359,-0.6039422883904094],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"SPX\",\"x\":[\"2018-03-01\",\"2018-06-01\",\"2018-09-01\",\"2018-12-01\",\"2019-03-01\",\"2019-06-01\",\"2019-09-01\",\"2019-12-01\",\"2020-03-01\",\"2020-06-01\",\"2020-09-01\",\"2020-12-01\",\"2021-03-01\",\"2021-06-01\",\"2021-09-01\",\"2021-12-01\",\"2022-03-01\",\"2022-06-01\",\"2022-09-01\"],\"y\":[0.0,0.015420421521022359,-0.09669349440683989,-0.091541890239276,-0.09831875648223753,-0.15889189005527926,-0.3238028751507137,-0.3021861133013793,-0.4153169108791584,-0.288250420951932,-0.2441834151882844,-0.1861432088236132,-0.1118765948442563,-0.1428880299543885,-0.15671367265554081,-0.08498116194286852,-0.0692509229950365,-0.07606337302593434,-0.060206992686251126,-0.026142709807686404,-0.00556563158054774,-0.009866779945955653,0.009431355567630549,0.07078980046219653,0.07539372073236464,0.16207370357320372,0.11977356856616517,0.12460094770881808,0.011489106540859231,0.061297015648956545,-0.03033636746102892,-0.6020273802156646,-0.7123812037659562,-0.4920115140550281,-0.4367331844041445,-0.33679269706742865,-0.3306531304903035,-0.37268131892762646,-0.36381325777868373,-0.25952419260997384,-0.18277481600651196,-0.17652165525332084,-0.26795985859146876,-0.2356920143971257,-0.14144053670126078,-0.21659461937637747,-0.12520989827404583,-0.11843103026845993,-0.05123309801836999,0.017778540707878085,0.019144138672787272,0.11485746525887455,0.1437046480208089,0.17703849961320217,0.21687140567779414,0.2412548538027845,0.26551717672687575,0.26894090065731174,0.16554721389655586,0.2553299706098052,0.192509989938916,0.25013794649499754,0.2830880334454756,0.2923163455026178,0.37782690751231596,0.39185952268808383,0.41063160050511543,0.47333263361815836,0.48657173784167357,0.5073973709724179,0.564918911826964,0.5137082611579536,0.5292306692846602,0.5104702201253923,0.5700643730171614,0.6383615353248421,0.5751434907588598,0.608363052177717,0.7418948557066227,0.7789738740245858,0.8403221968569479,0.911768439137425,0.9829539849179558,0.9805055677552764,0.9324870355041249,0.8824947682738887,0.8455213218848242],\"type\":\"scatter\"}],                        {\"template\":{\"data\":{\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"bar\":[{\"error_x\":{\"color\":\"#2a3f5f\"},\"error_y\":{\"color\":\"#2a3f5f\"},\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"baxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"type\":\"carpet\"}],\"choropleth\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"choropleth\"}],\"contourcarpet\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"contourcarpet\"}],\"contour\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"contour\"}],\"heatmapgl\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"heatmapgl\"}],\"heatmap\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"heatmap\"}],\"histogram2dcontour\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"histogram2dcontour\"}],\"histogram2d\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"histogram2d\"}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"mesh3d\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"type\":\"mesh3d\"}],\"parcoords\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"parcoords\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}],\"scatter3d\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatter3d\"}],\"scattercarpet\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattercarpet\"}],\"scattergeo\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattergeo\"}],\"scattergl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattergl\"}],\"scattermapbox\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scattermapbox\"}],\"scatterpolargl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatterpolargl\"}],\"scatterpolar\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatterpolar\"}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"scatterternary\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"type\":\"scatterternary\"}],\"surface\":[{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"type\":\"surface\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"#EBF0F8\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"#C8D4E3\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}]},\"layout\":{\"annotationdefaults\":{\"arrowcolor\":\"#2a3f5f\",\"arrowhead\":0,\"arrowwidth\":1},\"autotypenumbers\":\"strict\",\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"colorscale\":{\"diverging\":[[0,\"#8e0152\"],[0.1,\"#c51b7d\"],[0.2,\"#de77ae\"],[0.3,\"#f1b6da\"],[0.4,\"#fde0ef\"],[0.5,\"#f7f7f7\"],[0.6,\"#e6f5d0\"],[0.7,\"#b8e186\"],[0.8,\"#7fbc41\"],[0.9,\"#4d9221\"],[1,\"#276419\"]],\"sequential\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"sequentialminus\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]},\"colorway\":[\"#636efa\",\"#EF553B\",\"#00cc96\",\"#ab63fa\",\"#FFA15A\",\"#19d3f3\",\"#FF6692\",\"#B6E880\",\"#FF97FF\",\"#FECB52\"],\"font\":{\"color\":\"#2a3f5f\"},\"geo\":{\"bgcolor\":\"white\",\"lakecolor\":\"white\",\"landcolor\":\"#E5ECF6\",\"showlakes\":true,\"showland\":true,\"subunitcolor\":\"white\"},\"hoverlabel\":{\"align\":\"left\"},\"hovermode\":\"closest\",\"mapbox\":{\"style\":\"light\"},\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"#E5ECF6\",\"polar\":{\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"bgcolor\":\"#E5ECF6\",\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\"},\"yaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\"},\"zaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\"}},\"shapedefaults\":{\"line\":{\"color\":\"#2a3f5f\"}},\"ternary\":{\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"bgcolor\":\"#E5ECF6\",\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"title\":{\"x\":0.05},\"xaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"zerolinewidth\":2},\"yaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"zerolinewidth\":2}}},\"title\":{\"text\":\"Portfolio Cumulative Return Quarterly\"},\"xaxis\":{\"title\":{\"text\":\"Quarterly Trade Date\"}},\"yaxis\":{\"title\":{\"text\":\"Portfolio Value\"}}},                        {\"responsive\": true}                    ).then(function(){\n",
       "                            \n",
       "var gd = document.getElementById('9038a2f7-852f-4999-a7a4-655b2dee4242');\n",
       "var x = new MutationObserver(function (mutations, observer) {{\n",
       "        var display = window.getComputedStyle(gd).display;\n",
       "        if (!display || display === 'none') {{\n",
       "            console.log([gd, 'removed!']);\n",
       "            Plotly.purge(gd);\n",
       "            observer.disconnect();\n",
       "        }}\n",
       "}});\n",
       "\n",
       "// Listen for the removal of the full notebook cells\n",
       "var notebookContainer = gd.closest('#notebook-container');\n",
       "if (notebookContainer) {{\n",
       "    x.observe(notebookContainer, {childList: true});\n",
       "}}\n",
       "\n",
       "// Listen for the clearing of the current output cell\n",
       "var outputEl = gd.closest('.output');\n",
       "if (outputEl) {{\n",
       "    x.observe(outputEl, {childList: true});\n",
       "}}\n",
       "\n",
       "                        })                };                });            </script>        </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "trace_return1 = go.Scatter(x = time_ind, y = portfolio_cumsum_meanv, mode = 'lines', name = 'Mean-Variance')\n",
    "trace_return2 = go.Scatter(x = time_ind, y = portfolio_cumsum_minv, mode = 'lines', name = 'Minimum-Variance')\n",
    "trace_return3 = go.Scatter(x = time_ind, y = portfolio_cumsum_equal, mode = 'lines', name = 'Equally Weighted')\n",
    "trace_return4 = go.Scatter(x = time_ind, y = spx_cumsum, mode = 'lines', name = 'SPX')\n",
    "\n",
    "data = [trace_return1,trace_return2,trace_return3, trace_return4]\n",
    "layout = dict(title = \"Portfolio Cumulative Return Quarterly\", \n",
    "              xaxis = dict(title = 'Quarterly Trade Date'), \n",
    "              yaxis = dict(title = 'Portfolio Value'))\n",
    "fig = dict(data = data, layout = layout)\n",
    "plotly.offline.iplot(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. Get all Quarterly Return and Calcualte Annulized return, standard deviation and Sharpe ratio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "quarter_return = pd.DataFrame(index = range(len(portfolio_return_equal)), columns = ['Mean-Var', 'Equally', 'Min-Var'])\n",
    "quarter_return['SPX'] = spx_return\n",
    "quarter_return['Mean-Var'] = portfolio_return_meanv\n",
    "quarter_return['Equally'] = portfolio_return_equal\n",
    "quarter_return['Min-Var'] = portfolio_return_minv\n",
    "\n",
    "#writer_return = pd.ExcelWriter('quarter_return_user8.xlsx')\n",
    "quarter_return.index=tradedate_full\n",
    "#quarter_return.to_excel(writer_return,'Sheet1')\n",
    "#writer_return.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Mean-Var</th>\n",
       "      <th>Equally</th>\n",
       "      <th>Min-Var</th>\n",
       "      <th>SPX</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-03-01</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-06-01</th>\n",
       "      <td>0.02529</td>\n",
       "      <td>-0.01857</td>\n",
       "      <td>-0.006528</td>\n",
       "      <td>0.015420</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-09-01</th>\n",
       "      <td>-0.018328</td>\n",
       "      <td>0.018824</td>\n",
       "      <td>0.016548</td>\n",
       "      <td>-0.112114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-01</th>\n",
       "      <td>0.026316</td>\n",
       "      <td>0.026002</td>\n",
       "      <td>0.024862</td>\n",
       "      <td>0.005152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-03-01</th>\n",
       "      <td>-0.214458</td>\n",
       "      <td>-0.16628</td>\n",
       "      <td>-0.164245</td>\n",
       "      <td>-0.006777</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Mean-Var   Equally   Min-Var       SPX\n",
       "2018-03-01         0         0         0  0.000000\n",
       "2018-06-01   0.02529  -0.01857 -0.006528  0.015420\n",
       "2018-09-01 -0.018328  0.018824  0.016548 -0.112114\n",
       "2018-12-01  0.026316  0.026002  0.024862  0.005152\n",
       "2019-03-01 -0.214458  -0.16628 -0.164245 -0.006777"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "quarter_return.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "annulizedR_spx=quarter_return['SPX'].mean()*4\n",
    "annulizedR_meanv=quarter_return['Mean-Var'].mean()*4\n",
    "annulizedR_equal=quarter_return['Equally'].mean()*4\n",
    "annulizedR_minv=quarter_return['Min-Var'].mean()*4\n",
    "annulizedS_spx=quarter_return['SPX'].std()*2\n",
    "annulizedS_meanv=quarter_return['Mean-Var'].std()*2\n",
    "annulizedS_equal=quarter_return['Equally'].std()*2\n",
    "annulizedS_minv=quarter_return['Min-Var'].std()*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SPX -0.00522854196153727 0.13913436271090593\n",
      "Mean-Var -0.12512200765811338 0.5267684626050293\n",
      "Equally -0.12078845767808188 0.4858170592420743\n",
      "Min-Var -0.12281867413974365 0.4877126592890122\n"
     ]
    }
   ],
   "source": [
    "print(\"SPX\",annulizedR_spx,annulizedS_spx)\n",
    "print(\"Mean-Var\",annulizedR_meanv,annulizedS_meanv)\n",
    "print(\"Equally\",annulizedR_equal,annulizedS_equal)\n",
    "print(\"Min-Var\",annulizedR_minv,annulizedS_minv)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "risk_free=0.015"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "sharpe_spx=(annulizedR_spx-risk_free)/annulizedS_spx\n",
    "sharpe_meanv=(annulizedR_meanv-risk_free)/annulizedS_meanv\n",
    "sharpe_equal=(annulizedR_equal-risk_free)/annulizedS_equal\n",
    "sharpe_minv=(annulizedR_minv-risk_free)/annulizedS_minv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SPX Sharpe -0.1453885407415006\n",
      "Mean-Var Sharpe -0.2660030309429834\n",
      "Equally Sharpe -0.27950533044254594\n",
      "Min-Var Sharpe -0.2825817036216689\n"
     ]
    }
   ],
   "source": [
    "print(\"SPX Sharpe\",sharpe_spx)\n",
    "print(\"Mean-Var Sharpe\",sharpe_meanv)\n",
    "print(\"Equally Sharpe\",sharpe_equal)\n",
    "print(\"Min-Var Sharpe\",sharpe_minv)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "insample_date=tradedate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2001-03-01',\n",
       " '2001-06-01',\n",
       " '2001-09-01',\n",
       " '2001-12-01',\n",
       " '2002-03-01',\n",
       " '2002-06-01',\n",
       " '2002-09-01',\n",
       " '2002-12-01',\n",
       " '2003-03-01',\n",
       " '2003-06-01',\n",
       " '2003-09-01',\n",
       " '2003-12-01',\n",
       " '2004-03-01',\n",
       " '2004-06-01',\n",
       " '2004-09-01',\n",
       " '2004-12-01',\n",
       " '2005-03-01',\n",
       " '2005-06-01',\n",
       " '2005-09-01',\n",
       " '2005-12-01',\n",
       " '2006-03-01',\n",
       " '2006-06-01',\n",
       " '2006-09-01',\n",
       " '2006-12-01',\n",
       " '2007-03-01',\n",
       " '2007-06-01',\n",
       " '2007-09-01',\n",
       " '2007-12-01',\n",
       " '2008-03-01',\n",
       " '2008-06-01',\n",
       " '2008-09-01',\n",
       " '2008-12-01',\n",
       " '2009-03-01',\n",
       " '2009-06-01',\n",
       " '2009-09-01',\n",
       " '2009-12-01',\n",
       " '2010-03-01',\n",
       " '2010-06-01',\n",
       " '2010-09-01',\n",
       " '2010-12-01',\n",
       " '2011-03-01',\n",
       " '2011-06-01',\n",
       " '2011-09-01',\n",
       " '2011-12-01',\n",
       " '2012-03-01',\n",
       " '2012-06-01',\n",
       " '2012-09-01',\n",
       " '2012-12-01',\n",
       " '2013-03-01',\n",
       " '2013-06-01',\n",
       " '2013-09-01',\n",
       " '2013-12-01',\n",
       " '2014-03-01',\n",
       " '2014-06-01',\n",
       " '2014-09-01',\n",
       " '2014-12-01',\n",
       " '2015-03-01',\n",
       " '2015-06-01',\n",
       " '2015-09-01',\n",
       " '2015-12-01',\n",
       " '2016-03-01',\n",
       " '2016-06-01',\n",
       " '2016-09-01',\n",
       " '2016-12-01',\n",
       " '2017-03-01',\n",
       " '2017-06-01',\n",
       " '2017-09-01',\n",
       " '2017-12-01',\n",
       " '2018-03-01',\n",
       " '2018-06-01',\n",
       " '2018-09-01',\n",
       " '2018-12-01',\n",
       " '2019-03-01',\n",
       " '2019-06-01',\n",
       " '2019-09-01',\n",
       " '2019-12-01',\n",
       " '2020-03-01',\n",
       " '2020-06-01',\n",
       " '2020-09-01',\n",
       " '2020-12-01',\n",
       " '2021-03-01',\n",
       " '2021-06-01',\n",
       " '2021-09-01',\n",
       " '2021-12-01',\n",
       " '2022-03-01',\n",
       " '2022-06-01',\n",
       " '2022-09-01']"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "insample_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "\"['2001-03-01', '2001-06-01', '2001-09-01', '2001-12-01', '2002-03-01', '2002-06-01', '2002-09-01', '2002-12-01', '2003-03-01', '2003-06-01', '2003-09-01', '2003-12-01', '2004-03-01', '2004-06-01', '2004-09-01', '2004-12-01', '2005-03-01', '2005-06-01', '2005-09-01', '2005-12-01', '2006-03-01', '2006-06-01', '2006-09-01', '2006-12-01', '2007-03-01', '2007-06-01', '2007-09-01', '2007-12-01', '2008-03-01', '2008-06-01', '2008-09-01', '2008-12-01', '2009-03-01', '2009-06-01', '2009-09-01', '2009-12-01', '2010-03-01', '2010-06-01', '2010-09-01', '2010-12-01', '2011-03-01', '2011-06-01', '2011-09-01', '2011-12-01', '2012-03-01', '2012-06-01', '2012-09-01', '2012-12-01', '2013-03-01', '2013-06-01', '2013-09-01', '2013-12-01', '2014-03-01', '2014-06-01', '2014-09-01', '2014-12-01', '2015-03-01', '2015-06-01', '2015-09-01', '2015-12-01', '2016-03-01', '2016-06-01', '2016-09-01', '2016-12-01', '2017-03-01', '2017-06-01', '2017-09-01', '2017-12-01'] not in index\"",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m/home/wenbiaolin/20221117/fundamental_back_testing.ipynb Cell 65\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> <a href='vscode-notebook-cell://ssh-remote%2B10.26.1.22/home/wenbiaolin/20221117/fundamental_back_testing.ipynb#Y116sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a>\u001b[0m quarter_return[\u001b[39m'\u001b[39;49m\u001b[39mSPX\u001b[39;49m\u001b[39m'\u001b[39;49m][insample_date]\n",
      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/series.py:984\u001b[0m, in \u001b[0;36mSeries.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m    981\u001b[0m     key \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39masarray(key, dtype\u001b[39m=\u001b[39m\u001b[39mbool\u001b[39m)\n\u001b[1;32m    982\u001b[0m     \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_get_values(key)\n\u001b[0;32m--> 984\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_get_with(key)\n",
      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/series.py:1024\u001b[0m, in \u001b[0;36mSeries._get_with\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m   1021\u001b[0m         \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39miloc[key]\n\u001b[1;32m   1023\u001b[0m \u001b[39m# handle the dup indexing case GH#4246\u001b[39;00m\n\u001b[0;32m-> 1024\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mloc[key]\n",
      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/indexing.py:967\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m    964\u001b[0m axis \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39maxis \u001b[39mor\u001b[39;00m \u001b[39m0\u001b[39m\n\u001b[1;32m    966\u001b[0m maybe_callable \u001b[39m=\u001b[39m com\u001b[39m.\u001b[39mapply_if_callable(key, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mobj)\n\u001b[0;32m--> 967\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_getitem_axis(maybe_callable, axis\u001b[39m=\u001b[39;49maxis)\n",
      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/indexing.py:1191\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_axis\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m   1188\u001b[0m     \u001b[39mif\u001b[39;00m \u001b[39mhasattr\u001b[39m(key, \u001b[39m\"\u001b[39m\u001b[39mndim\u001b[39m\u001b[39m\"\u001b[39m) \u001b[39mand\u001b[39;00m key\u001b[39m.\u001b[39mndim \u001b[39m>\u001b[39m \u001b[39m1\u001b[39m:\n\u001b[1;32m   1189\u001b[0m         \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mCannot index with multidimensional key\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m-> 1191\u001b[0m     \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_getitem_iterable(key, axis\u001b[39m=\u001b[39;49maxis)\n\u001b[1;32m   1193\u001b[0m \u001b[39m# nested tuple slicing\u001b[39;00m\n\u001b[1;32m   1194\u001b[0m \u001b[39mif\u001b[39;00m is_nested_tuple(key, labels):\n",
      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/indexing.py:1132\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_iterable\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m   1129\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_validate_key(key, axis)\n\u001b[1;32m   1131\u001b[0m \u001b[39m# A collection of keys\u001b[39;00m\n\u001b[0;32m-> 1132\u001b[0m keyarr, indexer \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_get_listlike_indexer(key, axis)\n\u001b[1;32m   1133\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mobj\u001b[39m.\u001b[39m_reindex_with_indexers(\n\u001b[1;32m   1134\u001b[0m     {axis: [keyarr, indexer]}, copy\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m, allow_dups\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m\n\u001b[1;32m   1135\u001b[0m )\n",
      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/indexing.py:1327\u001b[0m, in \u001b[0;36m_LocIndexer._get_listlike_indexer\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m   1324\u001b[0m ax \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mobj\u001b[39m.\u001b[39m_get_axis(axis)\n\u001b[1;32m   1325\u001b[0m axis_name \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mobj\u001b[39m.\u001b[39m_get_axis_name(axis)\n\u001b[0;32m-> 1327\u001b[0m keyarr, indexer \u001b[39m=\u001b[39m ax\u001b[39m.\u001b[39;49m_get_indexer_strict(key, axis_name)\n\u001b[1;32m   1329\u001b[0m \u001b[39mreturn\u001b[39;00m keyarr, indexer\n",
      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py:5782\u001b[0m, in \u001b[0;36mIndex._get_indexer_strict\u001b[0;34m(self, key, axis_name)\u001b[0m\n\u001b[1;32m   5779\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m   5780\u001b[0m     keyarr, indexer, new_indexer \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_reindex_non_unique(keyarr)\n\u001b[0;32m-> 5782\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_raise_if_missing(keyarr, indexer, axis_name)\n\u001b[1;32m   5784\u001b[0m keyarr \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mtake(indexer)\n\u001b[1;32m   5785\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(key, Index):\n\u001b[1;32m   5786\u001b[0m     \u001b[39m# GH 42790 - Preserve name from an Index\u001b[39;00m\n",
      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py:5845\u001b[0m, in \u001b[0;36mIndex._raise_if_missing\u001b[0;34m(self, key, indexer, axis_name)\u001b[0m\n\u001b[1;32m   5842\u001b[0m     \u001b[39mraise\u001b[39;00m \u001b[39mKeyError\u001b[39;00m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mNone of [\u001b[39m\u001b[39m{\u001b[39;00mkey\u001b[39m}\u001b[39;00m\u001b[39m] are in the [\u001b[39m\u001b[39m{\u001b[39;00maxis_name\u001b[39m}\u001b[39;00m\u001b[39m]\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m   5844\u001b[0m not_found \u001b[39m=\u001b[39m \u001b[39mlist\u001b[39m(ensure_index(key)[missing_mask\u001b[39m.\u001b[39mnonzero()[\u001b[39m0\u001b[39m]]\u001b[39m.\u001b[39munique())\n\u001b[0;32m-> 5845\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mKeyError\u001b[39;00m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00mnot_found\u001b[39m}\u001b[39;00m\u001b[39m not in index\u001b[39m\u001b[39m\"\u001b[39m)\n",
      "\u001b[0;31mKeyError\u001b[0m: \"['2001-03-01', '2001-06-01', '2001-09-01', '2001-12-01', '2002-03-01', '2002-06-01', '2002-09-01', '2002-12-01', '2003-03-01', '2003-06-01', '2003-09-01', '2003-12-01', '2004-03-01', '2004-06-01', '2004-09-01', '2004-12-01', '2005-03-01', '2005-06-01', '2005-09-01', '2005-12-01', '2006-03-01', '2006-06-01', '2006-09-01', '2006-12-01', '2007-03-01', '2007-06-01', '2007-09-01', '2007-12-01', '2008-03-01', '2008-06-01', '2008-09-01', '2008-12-01', '2009-03-01', '2009-06-01', '2009-09-01', '2009-12-01', '2010-03-01', '2010-06-01', '2010-09-01', '2010-12-01', '2011-03-01', '2011-06-01', '2011-09-01', '2011-12-01', '2012-03-01', '2012-06-01', '2012-09-01', '2012-12-01', '2013-03-01', '2013-06-01', '2013-09-01', '2013-12-01', '2014-03-01', '2014-06-01', '2014-09-01', '2014-12-01', '2015-03-01', '2015-06-01', '2015-09-01', '2015-12-01', '2016-03-01', '2016-06-01', '2016-09-01', '2016-12-01', '2017-03-01', '2017-06-01', '2017-09-01', '2017-12-01'] not in index\""
     ]
    }
   ],
   "source": [
    "quarter_return['SPX'][insample_date]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SPX 0.001531531093822985 0.21955274735546973\n",
      "Mean-Var 0.2565791830967157 0.29155309241664823\n",
      "Equally 0.26616405706243595 0.3011567507349648\n",
      "Min-Var 0.26844700713719916 0.29823334992044453\n",
      "SPX Sharpe -0.061345025595925314\n",
      "Mean-Var Sharpe 0.828594137329483\n",
      "Equally Sharpe 0.8339977651155983\n",
      "Min-Var Sharpe 0.8498278519314074\n"
     ]
    }
   ],
   "source": [
    "annulizedR_spx_insample=quarter_return['SPX'][insample_date].mean()*4\n",
    "annulizedR_meanv_insample=quarter_return['Mean-Var'][insample_date].mean()*4\n",
    "annulizedR_equal_insample=quarter_return['Equally'][insample_date].mean()*4\n",
    "annulizedR_minv_insample=quarter_return['Min-Var'][insample_date].mean()*4\n",
    "annulizedS_spx_insample=quarter_return['SPX'][insample_date].std()*2\n",
    "annulizedS_meanv_insample=quarter_return['Mean-Var'][insample_date].std()*2\n",
    "annulizedS_equal_insample=quarter_return['Equally'][insample_date].std()*2\n",
    "annulizedS_minv_insample=quarter_return['Min-Var'][insample_date].std()*2\n",
    "print(\"SPX\",annulizedR_spx_insample,annulizedS_spx_insample)\n",
    "print(\"Mean-Var\",annulizedR_meanv_insample,annulizedS_meanv_insample)\n",
    "print(\"Equally\",annulizedR_equal_insample,annulizedS_equal_insample)\n",
    "print(\"Min-Var\",annulizedR_minv_insample,annulizedS_minv_insample)\n",
    "sharpe_spx_insample=(annulizedR_spx_insample-risk_free)/annulizedS_spx_insample\n",
    "sharpe_meanv_insample=(annulizedR_meanv_insample-risk_free)/annulizedS_meanv_insample\n",
    "sharpe_equal_insample=(annulizedR_equal_insample-risk_free)/annulizedS_equal_insample\n",
    "sharpe_minv_insample=(annulizedR_minv_insample-risk_free)/annulizedS_minv_insample\n",
    "print(\"SPX Sharpe\",sharpe_spx_insample)\n",
    "print(\"Mean-Var Sharpe\",sharpe_meanv_insample)\n",
    "print(\"Equally Sharpe\",sharpe_equal_insample)\n",
    "print(\"Min-Var Sharpe\",sharpe_minv_insample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
